package cn.wit.sst.utils;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Tooltip;
import javafx.util.Duration;

public class Dialogs {
	private static Logger logger = LoggerFactory.getLogger(Dialogs.class);

	public static Alert alert(String message) {
        Alert alert = new Alert(AlertType.WARNING);

        alert.setTitle("错误");
        alert.setHeaderText(null);
        alert.setContentText(message);

        alert.showAndWait();
        return alert;
	}

	public static Alert info(String message) {
        Alert alert = new Alert(AlertType.INFORMATION);

        alert.setTitle("提示");
        alert.setHeaderText(null);
        alert.setContentText(message);

        alert.showAndWait();
        return alert;
	}

	/**
	 * 控制tooltip的显示时间
	 *
	 * https://stackoverflow.com/questions/26854301/how-to-control-the-javafx-tooltips-delay
	 * @param text - tooltip显示的文字
	 * @return
	 */
	public static Tooltip tooltip(String text) {
		try {
			Tooltip obj = new Tooltip(text);

	        Field fieldBehavior = obj.getClass().getDeclaredField("BEHAVIOR");
	        fieldBehavior.setAccessible(true);
	        Object objBehavior = fieldBehavior.get(obj);

	        Field fieldTimer = objBehavior.getClass().getDeclaredField("activationTimer");
	        fieldTimer.setAccessible(true);
	        Timeline objTimer = (Timeline) fieldTimer.get(objBehavior);

	        objTimer.getKeyFrames().clear();
	        objTimer.getKeyFrames().add(new KeyFrame(new Duration(250)));

			return obj;
		} catch (Exception e) {
			logger.error("创建tooltip失败！", e);
			return null;
		}
	}
}
